<script lang="ts" setup>
import { ref, computed } from 'vue'

const hover = ref(false)
const props = defineProps({
  iconName: String
})
// 根据 iconName 计算默认图标路径
const iconUrl = computed(() => {
  return `button/${props.iconName}.png`
})
// 根据 iconName 计算悬浮时的图标路径
const iconHoverUrl = computed(() => {
  return `button/red${props.iconName}.png`
})

const emit = defineEmits(['click'])
const handleClick = () => {
  // 触发 'click' 事件
  emit('click')
}
</script>
<template>
  <div
    class="map-button is-hover"
    @click="handleClick"
    @mouseenter="hover = true"
    @mouseleave="hover = false"
  >
    <div class="map-button-icon">
      <slot name="icon">
        <!-- 动态绑定图片路径，根据 hover 状态切换 -->
        <img :src="hover ? iconHoverUrl : iconUrl" alt="" class="icon" />
      </slot>
    </div>
    <slot name="text">
      <span>车辆地图</span>
    </slot>
  </div>
</template>

<style scoped lang="scss">
.map-button {
  display: inline-block;
  white-space: nowrap;
  .map-button-icon {
    display: inline-block;
    line-height: 32px;
    img {
      vertical-align: middle;
      width: 20px;
      height: 20px;
    }
  }
  // span {
  //   vertical-align: middle;
  //   font-size: 14px;
  //   font-family: PingFang SC;
  //   font-weight: 400;
  //   line-height: 32px;
  //   color: rgba(0, 0, 0, 0.9);
  //   margin-left: 3px;
  // }
  :slotted(span),
  span {
    vertical-align: middle;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 32px;
    color: var(--xy-normal-text-color);
    margin-left: 3px;
  }
}
.is-hover:hover span,
.is-hover:hover :slotted(span) {
  color: var(--xy-primary-color);
}
.is-hover {
  cursor: pointer;
}
</style>
